library MD5Mine initializer init requires LegacySystem

	globals
		private player MD5user 
		private real MD5userX 
		private real MD5userY
		private real MD5endX 
		private real MD5endY 
		
		private group G = CreateGroup()
		private unit MD5Caster 
	endglobals
	
	private constant function MD5Range takes nothing returns real
	   return 1000.
	endfunction

	private function MD5KillWidget takes widget w returns nothing
		local real x0 = GetWidgetX(w)
		local real y0 = GetWidgetY(w)
		local real x1 = MD5userX
		local real y1 = MD5userY
		local real x2 = MD5endX
		local real y2 = MD5endY
		local real xcen = (x2+x1)/2
		local real ycen = (y2+y1)/2
		local real d = RAbsBJ( (x2-x1)*(y1-y0)-(x1-x0)*(y2-y1) )/MD5Range() //Magic for distance from point to line
		local real currentlife = GetWidgetLife(w)
		if d<150.0 and ((xcen-x0)*(xcen-x0)+(ycen-y0)*(ycen-y0)) < (MD5Range()*MD5Range()/4) then
			if currentlife>0.405 then
				call SetWidgetLife(w,currentlife-1500.)
			endif
		endif
	endfunction

	private function MD5MineItemDamage takes nothing returns nothing
	   call MD5KillWidget(GetEnumItem())
	endfunction

	private function MD5MineTreeKill takes nothing returns nothing
		call MD5KillWidget(GetEnumDestructable())
	endfunction

	private function MD5MineDamageAir takes nothing returns boolean
		if GetUnitFlyHeight(GetFilterUnit()) > 0 and GetUnitFlyHeight(GetFilterUnit()) < 770 then
			call UnitDamageTarget( MD5Caster, GetFilterUnit(), GetRandomReal(300.0,600.0), true, false, ATTACK_TYPE_SIEGE, DAMAGE_TYPE_NORMAL, WEAPON_TYPE_WHOKNOWS )
		endif
		return false
	endfunction
	
	private function MD5MineConditions takes nothing returns boolean
		return GetSpellAbilityId() =='A08C' and GetUnitTypeId(GetTriggerUnit()) == 'n00S'
	endfunction

	private function MD5Mine takes nothing returns nothing
		local unit mine = GetTriggerUnit()
		local real x1 = GetUnitX(mine)
		local real y1 = GetUnitY(mine)
		local real x2 = GetSpellTargetX()
		local real y2 = GetSpellTargetY()
		local real xcenter
		local real ycenter
		local rect r
		local unit dummy 
		local integer current = udg_Second
		local real dx = x2 - x1  //Normalizes target position to be MD5Range away
		local real dy = y2 - y1
		local real length = SquareRoot(dx*dx+dy*dy+.1)
		local integer i 
		local real a = Atan2(y2 - y1, x2 - x1)
		
		set dummy = CreateUnit(GetOwningPlayer(GetTriggerUnit()),'n00Z',x1,y1,0.0)
		call UnitAddAbility(mine,'Avul')
		call UnitApplyTimedLife(mine,'Bhwd',2.0)
		call UnitApplyTimedLife(dummy,'Bhwd',2.0)
		
		//call IssuePointOrder(dummy,"carrionswarm",x2,y2)
		set dx = MD5Range()*dx / length
		set dy = MD5Range()*dy / length
		set x2 = x1+dx
		set y2 = y1+dy
		
		set MD5user = GetOwningPlayer(mine)
		set MD5userX = x1
		set MD5userY = y1
		set MD5endX = x2
		set MD5endY = y2
		if current-udg_MD5Limit!=0 then
			set udg_soundpath = "Abilities\\Spells\\Orc\\Shockwave\\Shockwave.wav"
			call PlaySoundPath.execute()
		else
			set udg_MD5Limit = current
		endif

		call TriggerSleepAction(0.)
		
		set r = Rect(0,0,1,1)
		
		set xcenter = x1 + 50.0*Cos(a)
		set ycenter = y1 + 50.0*Sin(a)
		call SetRect(r,xcenter-100.0,ycenter-100.0, xcenter+100.0, ycenter+100.0)
		call GroupEnumUnitsInRect(G, r, Condition(function killexplosiveFast))
		
		set i = 1
		loop
			exitwhen i > 10
			set xcenter = x1 + I2R(i)*100.0*Cos(a)
			set ycenter = y1 + I2R(i)*100.0*Sin(a)
			call SetRect(r,xcenter-100.0,ycenter-100.0, xcenter+100.0, ycenter+100.0)
			
			set MD5Caster = dummy
			call EnumDestructablesInRect(r,null,function MD5MineTreeKill)
			call EnumItemsInRect(r,null,function MD5MineItemDamage)
			call GroupEnumUnitsInRect(G, r, Condition(function MD5MineDamageAir))
			
			
			set i = i + 1
		endloop
		
		
		call IssuePointOrder(dummy, "impale" ,x2,y2)
		
		call RemoveRect(r)
		set mine = null
		set dummy = null
	endfunction

	private function init takes nothing returns nothing
		local trigger t
		set t = CreateTrigger()
		call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_SPELL_EFFECT  )
		call TriggerAddCondition(t, Condition( function MD5MineConditions ) )
		call TriggerAddAction(t,function MD5Mine)
	endfunction

endlibrary